/**
 * $Id: OrganizationResource.java 6 2011-09-14 00:18:07Z adriftrock@gmail.com $
 */

package com.swehr.server.resources;

import com.swehr.server.models.Organization;
import com.swehr.server.models.ResultInfo;
import com.swehr.server.services.IPersonnelService;
import com.google.inject.Inject;
import com.sun.jersey.api.json.JSONWithPadding;
import org.apache.log4j.Logger;

import javax.inject.Singleton;
import javax.ws.rs.*;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * @author Rock Huang
 * @version 0.1
 */
@Singleton
@Path("/organization")
public class OrganizationResource {
    private final static Logger logger = Logger.getLogger(OrganizationResource.class);

    @Inject
    private IPersonnelService service;

    @GET
    @Produces({"application/x-javascript"})
    @Path("{id}")
    public JSONWithPadding getOrg(@PathParam("id") String id, @QueryParam("callback") String callback) {
        logger.debug("web service accept param by getOrg[GET] method: id => " + id);
        Organization org = service.getOrgRoot(id);
        return new JSONWithPadding(org, callback);
    }

    @GET
    @Produces({"application/x-javascript"})
    @Path("byorg")
    public JSONWithPadding getJsonp(@QueryParam("callback") String callback, @QueryParam("id") String id) {
        List<Organization> orgs = service.listSubOrg(id);
        return new JSONWithPadding(new GenericEntity<List<Organization>>(orgs){}, callback);
    }

    @GET
    @Produces({"application/x-javascript"})
    @Path("jsonp")
    public JSONWithPadding getJsonp(@QueryParam("callback") String callback) {
        List<Organization> orgs = service.list();
        return new JSONWithPadding(new GenericEntity<List<Organization>>(orgs){}, callback);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/save")
    public ResultInfo save(List<Organization> orgs) {
        logger.debug("Save service accept param by POST method: entity");
        logger.debug(orgs.size());
        return service.batchSave(orgs);
    }

    /**
     * To call this service will create a new Organization according to what
     * Organization object passed in. The client will obtain a returned object.
     * if the id of returned object equals or less than zero that means the
     * operation is unsuccessful.
     *
     * @return Organization Return the saved Organization.
     */
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Organization add(Organization org) {
        logger.debug("Add service accept param by POST method: Org =>" + org);
        service.add(org);
        return org;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/signin")
    public Organization signin(Organization org) {
        logger.debug("Signin service accept param by POST method: Org =>" + org);
        return service.signin(org);
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Organization updateOrganization(Organization org) {
        logger.debug("web service accept param by PUT method: Org => " + org);
        service.update(org);
        return org;
    }

    @DELETE
    @Path("{id}")
    public ResultInfo deleteOrganization(@PathParam("id") int id) {
        logger.debug("web service accept param by DELETE method: id => " + id);
        return service.delete(id);
    }

}
